package com.cy.jmh;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 1,time = 1)
@Measurement(iterations = 3,time = 1)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class JMHDemo2 {

	@State(Scope.Thread)
	public static class ConcurrentHashMap_Cy {
		Map<Long, String> map = new ConcurrentHashMap<>();
	}

	@Benchmark
	public void run_ConcurrentHashMap_Cy(ConcurrentHashMap_Cy cy) {
		for (int i = 0; i < 100; i++) {
			long l = System.nanoTime();
			cy.map.put(l,String.valueOf(l));
		}
	}

	@State(Scope.Thread)
	public static class Collections_synchronizedMap_Cy {
		Map<Long, String> map = Collections.synchronizedMap(new HashMap<>());
	}

	@Benchmark
	public void run_synchronizedMap_Cy(Collections_synchronizedMap_Cy cy) {
		for (int i = 0; i < 100; i++) {
			long l = System.nanoTime();
			cy.map.put(l,String.valueOf(l));
		}
	}

	@State(Scope.Thread)
	public static class ConcurrentSkipListMap_Cy {
		Map<Long, String> map = new ConcurrentSkipListMap<>();
	}
	@Benchmark
	public void run_ConcurrentSkipListMap_Cy(ConcurrentSkipListMap_Cy cy) {
		for (int i = 0; i < 100; i++) {
			long l = System.nanoTime();
			cy.map.put(l,String.valueOf(l));
		}
	}

	public static void main(String[] args) throws RunnerException {
		Options opt = new OptionsBuilder()
				.include(JMHDemo2.class.getSimpleName())
				.forks(1)
				.threads(4)
				.build();
		new Runner(opt).run();
	}
}
